package com.niguang.labour.admin.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.niguang.labour.admin.entity.WorkType;
import com.niguang.labour.admin.service.AdminLaborCompanySiteService;
import com.niguang.labour.admin.service.WorkTypeService;
import com.niguang.labour.common.constant.ResultCode;
import com.niguang.labour.common.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 劳务公司工地关联关系表 前端控制器
 * </p>
 *
 * @author
 * @since 2018-06-10
 */
@RestController
@RequestMapping("/adminLaborCompanySite/")
public class AdminLaborCompanySiteController {

    @Autowired
    private AdminLaborCompanySiteService adminLaborCompanySiteService;

    @Autowired
    private WorkTypeService workTypeService;

    /**
     * 模糊搜索用户名下工地
     *
     * @param siteName
     * @return
     */
    @GetMapping("searchLabor")
    Result searchLabor(String siteName) {
        return adminLaborCompanySiteService.searchLabor(siteName);
    }

    /**
     * 查询项目下班组
     *
     * @return
     */
    @GetMapping("searchSiteLabor")
    Result searchSiteLabor(@NotNull @RequestParam Long siteId) {
        return adminLaborCompanySiteService.searchSiteLabor(siteId);
    }


    /**
     * 获取工种
     *
     * @return
     */
    @GetMapping("getWorkType")
    public Result getWorkType() {
        EntityWrapper<WorkType> ew = new EntityWrapper<>();
        ew.eq(WorkType.DEL_FLAG, 0);
        List<WorkType> typeList = workTypeService.selectList(ew);
        if (CollectionUtils.isEmpty(typeList)) {
            return Result.build(ResultCode.SUCCESS, typeList);
        }
        List<Map<String, Object>> list = new ArrayList<>(typeList.size());
        typeList.forEach(p -> {
            Map<String, Object> typeMap = new HashMap<>(2);
            typeMap.put("workTypeId", p.getId());
            typeMap.put("workTypeName", p.getName());
            list.add(typeMap);
        });
        return Result.build(ResultCode.SUCCESS, list);
    }

    /**
     * 搜索用户信息
     *
     * @param userPhone
     * @return
     */
    @GetMapping("getLaborUser")
    public Result getLaborUser(String userPhone) {
        return adminLaborCompanySiteService.getLaborUser(userPhone);
    }


}
